package com.example.touch.wifipositioning;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Environment;

public class Parser {

	String folderPath;

	public Parser(String folderPath) {
		this.folderPath = folderPath;
	}

	public ArrayList<TraceEntry> parse() {
		ArrayList<TraceEntry> list = new ArrayList<TraceEntry>();

		ArrayList<File> files = getFilesInFolder(folderPath);

		for (File file : files) {
			try {
				BufferedReader reader = new BufferedReader(new FileReader(file));
				StringBuilder sb = new StringBuilder();

				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				String result = sb.toString();
				JSONObject jObject = new JSONObject(result);

				JSONArray sessionTraces = jObject.getJSONArray("SessionTraces");
				for (int i = 0; i < sessionTraces.length(); i++) {
					try {
						JSONObject sessionTrace = sessionTraces.getJSONObject(i);
						JSONObject jPosition = sessionTrace.getJSONObject("Position");
						int x = jPosition.getInt("X");
						int y = jPosition.getInt("Y");
						GeoPosition position = new GeoPosition(x, y);
						TraceEntry trace = new TraceEntry(position);

						SignalStrengthSamples signalStrengthsMap = new SignalStrengthSamples();
						JSONArray signalStrengthSamples = sessionTrace.getJSONArray("SignalStrengthSamples");
						for (int j = 0; j < signalStrengthSamples.length(); j++) {
							try {
								JSONObject signalStrengthSample = signalStrengthSamples.getJSONObject(j);
								String mac = signalStrengthSample.getString("MAC");
								MACAddress macAddress = MACAddress.parse(mac);
								String signal = signalStrengthSample.getString("SignalStrength");
								double signalStrength = Double.parseDouble(signal);
								signalStrengthsMap.put(macAddress, signalStrength);
							} catch (JSONException e) {
							}
						}
						trace.setSignalStrengthSamples(signalStrengthsMap);
						list.add(trace);
					} catch (JSONException e) {
					}
				}
				reader.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			HashMap<GeoPosition, SignalStrengthSamples> sortedTraceEntries = new HashMap<GeoPosition, SignalStrengthSamples>();
			for(TraceEntry entry : list){
				if (sortedTraceEntries.containsKey(entry.getPosition())) {
						sortedTraceEntries.get(entry.getPosition()).add(entry.getSignalStrengthSamples());
				} else {
					sortedTraceEntries.put(entry.getPosition(), new SignalStrengthSamples(entry.getSignalStrengthSamples()));
				}
			}
			
			list = new ArrayList<TraceEntry>();
			
			for (Map.Entry<GeoPosition, SignalStrengthSamples>  entry : sortedTraceEntries.entrySet()) {
				TraceEntry traceEntry = new TraceEntry(entry.getKey());
				traceEntry.setSignalStrengthSamples(entry.getValue());
				
				list.add(traceEntry);
			}
		}
		
		return list;
	}

	private ArrayList<File> getFilesInFolder(String folderPath) {
		String path = Environment.getExternalStorageDirectory().toString() + folderPath;
		File folder = new File(path);

		ArrayList<File> fileList = new ArrayList<File>();
		for (File fileEntry : folder.listFiles()) {
			fileList.add(fileEntry);
		}
		return fileList;
	}
}
